<?php
/**
 * @auther:周卫星
 * @className:LoginController.php
 * @projectName:train
 * @classDesc:
 * @date:2019/10/21
 * @time:15:19
 */

namespace App\Http\Controllers\admin;


use App\Http\Controllers\Controller;
use App\Model\SysUser;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /**
     * 登录功能
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function login(Request $request){
        if ($request -> isMethod('get')){
            return view('admin.login.login');
        }
        if ($request -> isMethod('post')){
            // 接收参数
            $admin_name = $request -> post('admin_name');
            $admin_pwd = $request -> post('password');
            $data = [
                'admin_name' => $admin_name,
                'admin_pwd' => $admin_pwd
            ];
            $sys_user = new SysUser();
            $bol = $sys_user -> sysUserLogin($data);
            if ($bol){
                $result = ['code' => 0, 'msg' => '登录成功'];
            }else{
                $result = ['code' => 1, 'msg' => '登录失败'];
            }
            // 与数据库进行比对，进行判断
            return response() -> json($result) -> setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }
    }

    /**
     * 退出登录
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function signout(Request $request)
    {
        if ($request -> isMethod('get')){
            $request -> session() -> forget('admin');
            return redirect(url('login'));
        }
    }
}
